Shader "Custom/魂_单色替换"
{
    Properties
    {
        //当然就是材质啦
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        //要被替换的目标颜色
        _TargetColor ("Target Color", Color) = (1, 1, 1, 1) 
        //要被替换的颜色范围
        _TargetRange ("Target Color Range", Range(0, 1)) = 0.01
        //替换成什么颜色
        _ReplaceColor ("Replace Color", Color) = (1, 1, 1, 1) 


    }
    SubShader
    {
        Tags {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
            "PreviewType" = "Plane"
            "CanUseSpriteAtlas" = "True"
        }

        Cull Off
        Lighting Off
        ZWrite Off
        Blend One OneMinusSrcAlpha


        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 2.0
            //#pragma multi_compile_instancing
            //#pragma multi_compile_local _ PIXELSNAP_ON
            //#pragma multi_compile _ ETC1_EXTERNAL_ALPHA
            //#include "UnitySprites.cginc"

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            sampler2D _MainTex;
            fixed4 _TargetColor;
            fixed _TargetRange;
            fixed4 _ReplaceColor;

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                //替换颜色
                if(col.a <= 0){
                    clip(-1);
                }
                else if( (col.r < _TargetColor.r+_TargetRange && col.r>_TargetColor.r-_TargetRange)
                    && (col.g < _TargetColor.g+_TargetRange && col.g>_TargetColor.g-_TargetRange)
                    && (col.b < _TargetColor.b+_TargetRange && col.b>_TargetColor.b-_TargetRange)
                ){
                    col = _ReplaceColor;
                }
                return col;
            }
            ENDCG
        }
    }
}

